﻿<%@ WebHandler Language="C#" Class="uploadImage" %>

using System;
using System.Web;
using System.IO;
using Newtonsoft.Json;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using OneShoppingUtility;
using System.Collections.Generic;
using OneShoppingModel;
using OneShoppingBLL;
using OneShoppingDAL;
using System.Web.SessionState;

public class uploadImage : IHttpHandler, IRequiresSessionState
{
    
    public void ProcessRequest (HttpContext context)
    {
        SystemUser user =(SystemUser) context.Session["user"];
        if(user!=null)
        { 
            HttpPostedFile file = context.Request.Files[0];
            string data = context.Request["avatar_data"];
            ImageInfo imageinfo = JsonHelper.DeserializeJsonToObject<ImageInfo>(data);
            if (file != null)
            {
                Bitmap b = new Bitmap(file.InputStream);
                try
                {
                    Bitmap bitmap = UtilityImage.Cut(b, (int)imageinfo.x, (int)imageinfo.y, (int)imageinfo.width, (int)imageinfo.height);
                    //图片保存的文件夹路径
                    string path = context.Server.MapPath("../../uploadimages/");
                    //如果文件夹不存在，则创建
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    //上传图片的扩展名
                    string type = file.FileName.Substring(file.FileName.LastIndexOf('.'));
                    //保存图片的文件名
                    string saveName = Guid.NewGuid().ToString() + type;
                    //保存图片
                //    file.SaveAs(path  + saveName);
                    bitmap.Save(path + saveName);
                    string result = "uploadimages/" + saveName;

                    GeneralUser g = GeneralUserBLL.GetInstance().GetEntity(r => r.UserId == user.UserId);
                    if(g!=null)
                    {
                        g.HeadImage=result;
                        bool r= GeneralUserBLL.GetInstance().Update(g);
                        if(!r)
                        {
                            context.Response.Write(" ");
                        }
                        else
                        {
                            context.Response.Write("{\"result\":\"" + result + "\"}");
                        }
                    }
                    else
                    {
                        context.Response.Write("{\"result\":\"" + result + "\"}");
                    }
                }
                catch
                {
                    context.Response.Write(" ");
                }
            }
        }
        else
        {
            context.Response.Write(" ");
        }

    }

    class ImageInfo
    {
        public double x { get; set; }
        public double y { get; set; }
        public double height { get; set; }
        public double width { get; set; }
        public int rortate { get; set; }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}